﻿
Partial Class WebForm_ManageSalesForecast_SellIn
    Inherits System.Web.UI.Page
    Dim cmanager As New ChannelManager

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Request.QueryString("SapCode") IsNot Nothing Then
                Me.SapCode = Request.QueryString.Item("SapCode").ToString().Replace(MyObj.GetAreaCode(), "")
                Me.BindData()
            End If
            If Request.QueryString("Stock") IsNot Nothing Then
                Me.btnCancel.Visible = False
            End If
            Select Case Me.HeaderTB
                Case "SO_"
                    Me.PageTitle.Text = "Sell-Out (D-1)"
                Case "PLV_"
                    Me.PageTitle.Text = "Issue QTY(D-1)"
                Case Else
                    Me.PageTitle.Text = "Sell-In (D-1)"
            End Select
        End If
    End Sub

    Private Sub BindData()
        Dim sql As New StringBuilder
        Dim dat As New clsData

        Dim SkuType As String = ""
        Select Case Me.HeaderTB
            Case "SO_"
                SkuType = "SellOut"
            Case "PLV_"
                SkuType = "S"
            Case Else
                SkuType = "S"
        End Select

        sql.Length = 0
        sql.AppendFormat("select a.* ")
        sql.AppendFormat(",isnull(cast(S.MTD_S as varchar),'NA') as MTD_S ")
        sql.AppendFormat(",isnull(cast(PBOM.MTD_S as varchar),'NA') as MTD_PBOM ")
        sql.AppendFormat(",null as MTD_ALL ")
        sql.AppendFormat("from ProductData a ")
        sql.AppendFormat("left join ")
        sql.AppendFormat("( ")
        sql.AppendFormat("  select Sapcode,Cast(sum(isnull(Mon{0},0)) as int) as MTD_S from SFReport{1}A ", DateTime.Today.Month, DateTime.Today.Year)
        sql.AppendFormat("  where 1=1 ")
        sql.AppendFormat("  and Sapcode='{0}' ", Me.SapCode)
        sql.AppendFormat("  and Skutype='{0}' ", SkuType)
        sql.AppendFormat("  and ChannelID IN ({0}) ", MyObj.JoinFormat(",", "'{0}'", Me.ChlData))
        sql.AppendFormat("  group by Sapcode ")
        sql.AppendFormat(") S on a.Sapcode=S.Sapcode ")

        If SkuType = "S" Then
            sql.AppendFormat("left join ")
            sql.AppendFormat("( ")
            sql.AppendFormat("  select Sapcode,Cast(sum(isnull(Mon{0},0)) as int) as MTD_S from SFReport{1}A ", DateTime.Today.Month, DateTime.Today.Year)
            sql.AppendFormat("  where 1=1 ")
            sql.AppendFormat("  and Sapcode='{0}' ", Me.SapCode)
            sql.AppendFormat("  and Skutype='{0}' ", "PBOM")
            sql.AppendFormat("  and ChannelID IN ({0}) ", MyObj.JoinFormat(",", "'{0}'", Me.ChlData))
            sql.AppendFormat("  group by Sapcode ")
            sql.AppendFormat(") PBOM on a.Sapcode=PBOM.Sapcode ")
        Else
            sql.AppendFormat("left join ")
            sql.AppendFormat("( ")
            sql.AppendFormat("  select a.BomSapcode as Sapcode,Cast(sum(isnull(b.Mon{0},0)*isnull(a.QTY,0)) as int) as MTD_S from ProductBom{1} a ", DateTime.Today.Month, Me.MonthCode)
            sql.AppendFormat("  join SFReport{0}A b on a.SapCode=b.SapCode ", DateTime.Today.Year)
            sql.AppendFormat("  where 1=1 ")
            sql.AppendFormat("  and a.BomSapcode='{0}' ", Me.SapCode)
            sql.AppendFormat("  and b.Skutype='{0}' ", SkuType)
            sql.AppendFormat("  and b.ChannelID IN ({0}) ", MyObj.JoinFormat(",", "'{0}'", Me.ChlData))
            sql.AppendFormat("  group by a.BomSapcode ")
            sql.AppendFormat(") PBOM on a.Sapcode=PBOM.Sapcode ")
        End If

        sql.AppendFormat("where a.Sapcode='{0}' ", Me.SapCode)
        'MyObj.WriteLog(sql.ToString())
        Me.fv.DataSource = dat.GetData(sql.ToString())
        Me.fv.DataBind()
    End Sub

    Protected Sub fv_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles fv.DataBound
        If fv.DataMember IsNot Nothing Then
            Dim sql As New StringBuilder
            Dim dat As New clsData

            sql.Length = 0
            sql.AppendFormat("select distinct a.ChannelName,isnull(cast(b.MTD_S as varchar),'NA') as Stock from ChannelView a ")
            sql.AppendFormat("left join ")
            sql.AppendFormat("( ")
            sql.AppendFormat("  select Sapcode,ChannelID,sum(cast(Mon{0} as int)) as MTD_S from SFReport{1}A ", DateTime.Today.Month, DateTime.Today.Year)
            sql.AppendFormat("  where 1=1 ")
            sql.AppendFormat("  and Sapcode='{0}' ", Me.SapCode)
            Select Case Me.HeaderTB
                Case "SO_"
                    sql.AppendFormat("  and Skutype in ('SellOut') ")
                Case "PLV_"
                    sql.AppendFormat("  and Skutype in ('S','PBOM') ")
                Case Else
                    sql.AppendFormat("  and Skutype in ('S','PBOM') ")
            End Select
            sql.AppendFormat("  and ChannelID IN ({0}) ", MyObj.JoinFormat(",", "'{0}'", Me.ChlData))
            sql.AppendFormat("  group by Sapcode,ChannelID ")
            sql.AppendFormat(") b on b.SapCode='{0}' ", Me.SapCode)
            sql.AppendFormat("  and a.Channel=b.ChannelID  ")
            sql.AppendFormat("where a.Division='{0}' ", Session(Me.SessionID & "SF_Division"))
            sql.AppendFormat("and a.Signature='{0}' ", Session(Me.SessionID & "SF_Signature"))

            'MyObj.WriteLog(sql.ToString())

            TryCast(Me.fv.FindControl("rptTradeStock"), DataList).DataSource = dat.GetData(sql.ToString())
            TryCast(Me.fv.FindControl("rptTradeStock"), DataList).DataBind()

            Dim lblMTDValue_ALL As Label = TryCast(Me.fv.FindControl("lblMTDValue_ALL"), Label)
            Dim lblMTDValue_S As Label = TryCast(Me.fv.FindControl("lblMTDValue_S"), Label)
            Dim lblMTDValue_PBOM As Label = TryCast(Me.fv.FindControl("lblMTDValue_PBOM"), Label)
            If lblMTDValue_S.Text = "NA" And lblMTDValue_PBOM.Text = "NA" Then
                lblMTDValue_ALL.Text = "NA"
            Else
                Dim MTD_ALL As Integer = 0
                If IsNumeric(lblMTDValue_S.Text) Then
                    MTD_ALL += lblMTDValue_S.Text
                End If
                If IsNumeric(lblMTDValue_PBOM.Text) Then
                    MTD_ALL += lblMTDValue_PBOM.Text
                End If
                lblMTDValue_ALL.Text = MTD_ALL.ToString()
            End If

            '更改Text
            Dim lblMTD_ALL As Label = TryCast(Me.fv.FindControl("lblMTD_ALL"), Label)
            Dim lblMTD_PBOM As Label = TryCast(Me.fv.FindControl("lblMTD_PBOM"), Label)
            Dim lblMTD_S As Label = TryCast(Me.fv.FindControl("lblMTD_S"), Label)
            Select Case Me.HeaderTB
                Case "SO_"
                    lblMTD_ALL.Text = "Sell-Out(MTD)："
                    lblMTD_PBOM.Text = "Sell-Out-PBOM(MTD)："
                    lblMTD_S.Text = "Sell-Out-S(MTD)："
                Case "PLV_"
                    lblMTD_ALL.Text = "Issue Qty(MTD)："
                    lblMTD_PBOM.Text = "Issue Qty-PBOM(MTD)："
                    lblMTD_S.Text = "Issue Qty-S(MTD)："
                Case Else
                    lblMTD_ALL.Text = "Sell-in(MTD)："
                    lblMTD_PBOM.Text = "Sell-in-PBOM(MTD)："
                    lblMTD_S.Text = "Sell-in-S(MTD)："
            End Select
        End If
    End Sub

#Region "屬性"
    Private Property SapCode() As String
        Get
            Return ViewState("SapCode")
        End Get
        Set(ByVal value As String)
            ViewState("SapCode") = value
        End Set
    End Property

    Protected ReadOnly Property Division() As String
        Get
            Return Session(Me.SessionID & "SF_Division")
        End Get
    End Property

    Protected ReadOnly Property Signature() As String
        Get
            Return Session(Me.SessionID & "SF_Signature")
        End Get
    End Property

    Protected ReadOnly Property Channel() As String
        Get
            Return Session(Me.SessionID & "SF_Channel")
        End Get
    End Property

    Public ReadOnly Property SessionID() As String
        Get
            Return Me.Request.QueryString.Item("SessionID")
        End Get
    End Property
#End Region

    Dim stManager As New SignatureManager
#Region "Group Property"
    Private _ChlData As List(Of String)
    Protected ReadOnly Property ChlData() As List(Of String)
        Get
            If Me._ChlData Is Nothing Then
                Me._ChlData = cmanager.GetFullBySignature(Me.Signature)
                'If Me.Channel = String.Empty Then
                '    Me._ChlData = Me.cmanager.GetGroupChannels(Me.Division, Me.Signature, MyObj.SelectAllValue)
                'Else
                '    Me._ChlData = Me.cmanager.GetGroupChannels(Me.Division, Me.Signature, Me.Channel)
                'End If
            End If
            Return Me._ChlData
        End Get
    End Property

    '判斷是否為All或Group Channel
    Protected ReadOnly Property IsMutiChannel() As Boolean
        Get
            If Me.Channel = String.Empty OrElse Me.Channel.IndexOf(MyObj.GroupMark) <> -1 Then
                Return True
            Else
                Return False
            End If
        End Get
    End Property
#End Region

    Protected ReadOnly Property FCSTMonth() As String
        Get
            Return Session(Me.SessionID & "SF_FCSTMonth")
        End Get
    End Property

    Protected ReadOnly Property MonthCode() As String
        Get
            Return Session(Me.SessionID & "SF_MonthCode")
        End Get
    End Property

    Public ReadOnly Property HeaderTB() As String
        Get
            Return Session(Me.SessionID & "SF_HeaderTB")
        End Get
    End Property

    Public ReadOnly Property ViewPoint() As String
        Get
            Return Session(Me.SessionID & "SF_ViewPoint")
        End Get
    End Property
End Class
